import * as PIXILEGACY from "pixi.js-legacy";
// import {PixiApp} from "@/pixi/App";
import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
// 虚线容器
import {dashContainer} from '@/pixi/dashContainer'

export class Dash{
    constructor(params) {
        this.tilingSprite = null;
        this.stopAnimationFrameLine = null;
        this.initDash(params)
    }

    static createDash(params){
        return new Dash(params)
    }

    initDash({x0, y0, x1, y1, linewidth,color='#000000',evlength=8}) {
        let store = usePixiStore()
        store.changeCurrentWidth()
        store.changeCurrentHeight()
        let {pixiMaxWidth,pixiMaxHeight,currentWidth,currentHeight } = storeToRefs(store)

        let dashed = new PIXILEGACY.Graphics();
        dashed.lineStyle(1/pixiMaxHeight.value*currentHeight.value, color, 1); // linewidth,color,alpha
        dashed.moveTo(0, 0);
        dashed.lineTo(evlength/pixiMaxHeight.value*currentHeight.value, 0);
        dashed.moveTo(evlength/pixiMaxHeight.value*currentHeight.value*2, 0);
        dashed.lineTo(evlength/pixiMaxHeight.value*currentHeight.value*3, 0);
        // dashed.lineTo(linewidth, 0);
        // dashed.moveTo(linewidth * 1.5, 0);
        // dashed.lineTo(linewidth * 2.5, 0);
        let dashedtexture = dashed.generateCanvasTexture(1, 1);


        let linelength = Math.pow(Math.pow(x1/pixiMaxWidth.value*currentWidth.value - x0/pixiMaxWidth.value*currentWidth.value, 2) + Math.pow(y1/pixiMaxHeight.value*currentHeight.value - y0/pixiMaxHeight.value*currentHeight.value, 2), 0.5);
        this.tilingSprite = new PIXILEGACY.TilingSprite(dashedtexture, linelength, linewidth/pixiMaxWidth.value*currentWidth.value);

        this.tilingSprite.x = x0/pixiMaxWidth.value*currentWidth.value - currentWidth.value/2 + (linewidth/pixiMaxWidth.value*currentWidth.value)/2;
        this.tilingSprite.y = y0/pixiMaxHeight.value*currentHeight.value - currentHeight.value/2 + (linewidth/pixiMaxHeight.value*currentHeight.value)/2;

        this.tilingSprite.rotation = this.angle(x0/pixiMaxWidth.value*currentWidth.value, y0/pixiMaxHeight.value*currentHeight.value, x1/pixiMaxWidth.value*currentWidth.value, y1/pixiMaxHeight.value*currentHeight.value) * Math.PI / 180;
        this.tilingSprite.pivot.set(linewidth/pixiMaxWidth.value*currentWidth.value / 2, linewidth/pixiMaxWidth.value*currentWidth.value / 2);

        dashContainer.addChild(this.tilingSprite)

        // 默认是开启 循环的
        this.loop()
        return this.tilingSprite;
    }

    angle(x0, y0, x1, y1) {
        let diff_x = Math.abs(x1 - x0),
            diff_y = Math.abs(y1 - y0);
        let cita;
        if (x1 > x0) {
            if (y1 > y0) {
                cita = 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
            } else {
                if (y1 < y0) {
                    cita = -360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                } else {
                    cita = 0;
                }
            }
        } else {
            if (x1 < x0) {
                if (y1 > y0) {
                    cita = 180 - 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                } else {
                    if (y1 < y0) {
                        cita = 180 + 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                    } else {
                        cita = 180;
                    }
                }
            } else {
                if (y1 > y0) {
                    cita = 90;
                } else {
                    if (y1 < y0) {
                        cita = -90;
                    } else {
                        cita = 0;
                    }
                }
            }
        }
        return cita;
    }

    loop(){
        this.tilingSprite.tilePosition.x  += 1;
        this.stopAnimationFrameLine = window.requestAnimationFrame(()=>{
            this.loop()
        });
    }

    stopLoop(){
        if(this.stopAnimationFrameLine){
            window.cancelAnimationFrame(this.stopAnimationFrameLine);
        }
    }

}
